package powerga.transmission.minloss;

import static org.junit.Assert.*;

import org.interpss.IpssPlugin;
import org.interpss.PluginObjectFactory;
import org.interpss.custom.IpssFileAdapter;
import org.jenetics.Genotype;
import org.jenetics.Integer64Chromosome;
import org.jenetics.Integer64Gene;
import org.junit.Test;

import com.interpss.common.exp.InterpssException;
import com.interpss.core.aclf.AclfBranch;
import com.interpss.core.aclf.AclfBus;
import com.interpss.core.aclf.AclfNetwork;
import com.interpss.core.net.Branch;
import com.interpss.core.net.Bus;

public class SimplePLossFunctionTest {

	@Test
	public void testEvaluate() throws InterpssException, Exception {
		// 1. Initialize the network
		IpssPlugin.init();		
		AclfNetwork net = PluginObjectFactory.getFileAdapter(IpssFileAdapter.FileFormat.IEEECDF).loadDebug("testData/ieee14.ieee").getAclfNet();
		// 2. Define the chromosome
		// 2.1. find the length of the chromosome
		int loadNumber = 0, transNumber = 0;
		// 2.1.1. number of buses to be compensated by capacitors
		for (Bus thisBus : net.getBusList()) {
			AclfBus thisAclfBus = net.getAclfBus(thisBus.getId());
			if (thisAclfBus.isLoad())
				loadNumber++;
		}
		// 2.1.2. number of transformers to be adjusted
		for (Branch thisBranch : net.getBranchList()) {
			AclfBranch thisAclfBranch = net.getAclfBranch(thisBranch.getId());
			if (thisAclfBranch.isXfr())
				transNumber++;
		}
		// 2.1.3. calculate the chromosome length
		int chromLength = loadNumber + transNumber;
		// 2.2. Generate a new chromosome
        Integer64Chromosome c1 = new Integer64Chromosome(0, chromLength - 1, chromLength);
        Genotype<Integer64Gene> g1 = Genotype.valueOf(c1);
        // 3. Test the calculation of the fitness function
		SimplePLossFunction myPLoss = new SimplePLossFunction(net, loadNumber, transNumber, 20, 9);
		Double evalPLoss = myPLoss.evaluate(g1);
		System.out.println("Totally active power amount is " + evalPLoss + "kW.");
		assertTrue((evalPLoss != null) && (evalPLoss != 0.0));
	}

}